using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Medianamik.SubsonicDALProvider.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Type class.
	/// </summary>
	[Serializable]
	public partial class TypeCollection : ActiveList<Type,TypeCollection> 
	{	   
		public TypeCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the medianamik_Type table.
	/// </summary>
	[Serializable]
	public partial class Type : ActiveRecord<Type>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Type()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Type(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

        
		public Type(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Type(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }

		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("medianamik_Type", TableType.Table, DataService.GetInstance("MedianamikProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarTypeId = new TableSchema.TableColumn(schema);
				colvarTypeId.ColumnName = "TypeId";
				colvarTypeId.DataType = DbType.Guid;
				colvarTypeId.MaxLength = 0;
				colvarTypeId.AutoIncrement = false;
				colvarTypeId.IsNullable = false;
				colvarTypeId.IsPrimaryKey = true;
				colvarTypeId.IsForeignKey = false;
				colvarTypeId.IsReadOnly = false;
				colvarTypeId.DefaultSetting = @"";
				colvarTypeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTypeId);
				
				TableSchema.TableColumn colvarParentTypeId = new TableSchema.TableColumn(schema);
				colvarParentTypeId.ColumnName = "ParentTypeId";
				colvarParentTypeId.DataType = DbType.Guid;
				colvarParentTypeId.MaxLength = 0;
				colvarParentTypeId.AutoIncrement = false;
				colvarParentTypeId.IsNullable = true;
				colvarParentTypeId.IsPrimaryKey = false;
				colvarParentTypeId.IsForeignKey = true;
				colvarParentTypeId.IsReadOnly = false;
				colvarParentTypeId.DefaultSetting = @"";
				
					colvarParentTypeId.ForeignKeyTableName = "medianamik_Type";
				schema.Columns.Add(colvarParentTypeId);
				
				TableSchema.TableColumn colvarModuleId = new TableSchema.TableColumn(schema);
				colvarModuleId.ColumnName = "ModuleId";
				colvarModuleId.DataType = DbType.Guid;
				colvarModuleId.MaxLength = 0;
				colvarModuleId.AutoIncrement = false;
				colvarModuleId.IsNullable = false;
				colvarModuleId.IsPrimaryKey = false;
				colvarModuleId.IsForeignKey = false;
				colvarModuleId.IsReadOnly = false;
				colvarModuleId.DefaultSetting = @"";
				colvarModuleId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModuleId);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.String;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarCanHaveChilds = new TableSchema.TableColumn(schema);
				colvarCanHaveChilds.ColumnName = "CanHaveChilds";
				colvarCanHaveChilds.DataType = DbType.Boolean;
				colvarCanHaveChilds.MaxLength = 0;
				colvarCanHaveChilds.AutoIncrement = false;
				colvarCanHaveChilds.IsNullable = false;
				colvarCanHaveChilds.IsPrimaryKey = false;
				colvarCanHaveChilds.IsForeignKey = false;
				colvarCanHaveChilds.IsReadOnly = false;
				colvarCanHaveChilds.DefaultSetting = @"";
				colvarCanHaveChilds.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCanHaveChilds);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MedianamikProvider"].AddSchema("medianamik_Type",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("TypeId")]
		public Guid TypeId 
		{
			get { return GetColumnValue<Guid>(Columns.TypeId); }

			set { SetColumnValue(Columns.TypeId, value); }

		}

		  
		[XmlAttribute("ParentTypeId")]
		public Guid? ParentTypeId 
		{
			get { return GetColumnValue<Guid?>(Columns.ParentTypeId); }

			set { SetColumnValue(Columns.ParentTypeId, value); }

		}

		  
		[XmlAttribute("ModuleId")]
		public Guid ModuleId 
		{
			get { return GetColumnValue<Guid>(Columns.ModuleId); }

			set { SetColumnValue(Columns.ModuleId, value); }

		}

		  
		[XmlAttribute("Name")]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }

			set { SetColumnValue(Columns.Name, value); }

		}

		  
		[XmlAttribute("CanHaveChilds")]
		public bool CanHaveChilds 
		{
			get { return GetColumnValue<bool>(Columns.CanHaveChilds); }

			set { SetColumnValue(Columns.CanHaveChilds, value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		private Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection colHierarchicalRuleRecords;
		public Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection HierarchicalRuleRecords()
		{
			if(colHierarchicalRuleRecords == null)
				colHierarchicalRuleRecords = new Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection().Where(HierarchicalRule.Columns.TypeId, TypeId).Load();
			return colHierarchicalRuleRecords;
		}

		private Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection colHierarchicalRuleRecordsFromType;
		public Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection HierarchicalRuleRecordsFromType()
		{
			if(colHierarchicalRuleRecordsFromType == null)
				colHierarchicalRuleRecordsFromType = new Medianamik.SubsonicDALProvider.DAL.HierarchicalRuleCollection().Where(HierarchicalRule.Columns.ParentTypeId, TypeId).Load();
			return colHierarchicalRuleRecordsFromType;
		}

		private Medianamik.SubsonicDALProvider.DAL.PropertyXCollection colPropertyXRecords;
		public Medianamik.SubsonicDALProvider.DAL.PropertyXCollection PropertyXRecords()
		{
			if(colPropertyXRecords == null)
				colPropertyXRecords = new Medianamik.SubsonicDALProvider.DAL.PropertyXCollection().Where(PropertyX.Columns.TypeId, TypeId).Load();
			return colPropertyXRecords;
		}

		private Medianamik.SubsonicDALProvider.DAL.TypeCollection colChildTypeRecords;
		public Medianamik.SubsonicDALProvider.DAL.TypeCollection ChildTypeRecords()
		{
			if(colChildTypeRecords == null)
				colChildTypeRecords = new Medianamik.SubsonicDALProvider.DAL.TypeCollection().Where(Type.Columns.ParentTypeId, TypeId).Load();
			return colChildTypeRecords;
		}

		private Medianamik.SubsonicDALProvider.DAL.ModuleTypeCollection colModuleTypeRecords;
		public Medianamik.SubsonicDALProvider.DAL.ModuleTypeCollection ModuleTypeRecords()
		{
			if(colModuleTypeRecords == null)
				colModuleTypeRecords = new Medianamik.SubsonicDALProvider.DAL.ModuleTypeCollection().Where(ModuleType.Columns.TypeId, TypeId).Load();
			return colModuleTypeRecords;
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Type ActiveRecord object related to this Type
		/// 
		/// </summary>
		public Medianamik.SubsonicDALProvider.DAL.Type ParentType
		{
			get { return Medianamik.SubsonicDALProvider.DAL.Type.FetchByID(this.ParentTypeId); }

			set { SetColumnValue("ParentTypeId", value.TypeId); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varTypeId,Guid? varParentTypeId,Guid varModuleId,string varName,bool varCanHaveChilds)
		{
			Type item = new Type();
			
			item.TypeId = varTypeId;
			
			item.ParentTypeId = varParentTypeId;
			
			item.ModuleId = varModuleId;
			
			item.Name = varName;
			
			item.CanHaveChilds = varCanHaveChilds;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varTypeId,Guid? varParentTypeId,Guid varModuleId,string varName,bool varCanHaveChilds)
		{
			Type item = new Type();
			
				item.TypeId = varTypeId;
			
				item.ParentTypeId = varParentTypeId;
			
				item.ModuleId = varModuleId;
			
				item.Name = varName;
			
				item.CanHaveChilds = varCanHaveChilds;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn TypeIdColumn
        {
            get { return Schema.Columns[0]; }

        }

        
        
        
        public static TableSchema.TableColumn ParentTypeIdColumn
        {
            get { return Schema.Columns[1]; }

        }

        
        
        
        public static TableSchema.TableColumn ModuleIdColumn
        {
            get { return Schema.Columns[2]; }

        }

        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[3]; }

        }

        
        
        
        public static TableSchema.TableColumn CanHaveChildsColumn
        {
            get { return Schema.Columns[4]; }

        }

        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string TypeId = @"TypeId";
			 public static string ParentTypeId = @"ParentTypeId";
			 public static string ModuleId = @"ModuleId";
			 public static string Name = @"Name";
			 public static string CanHaveChilds = @"CanHaveChilds";
						
		}

		#endregion
		
		
		
	}

}

